//setup overlay with height
var setOverlayInterval;
$(document).ready(function(){
	//setTimeout(setupOverlayWidthHeight, 2000);
	setOverlayInterval = setInterval(setupOverlayWidthHeight, 100);
});

function setupOverlayWidthHeight() {
	var	w = $("#originalLogo").width();
	var	h = $("#originalLogo").height();
	var num = Math.min(w, h);
	if (num > 10) {
		num = num - 10;
		$("#imgOverlay").width(num);
		$("#imgOverlay").height(num);
		clearInterval(setOverlayInterval);
	}
}

function enlarge() {
	var left = $("#imgOverlay").position().left;
	if (left > 0) {
		left--;
		$("#imgOverlay").css("left", (left) + "px");
	}
	var top = $("#imgOverlay").position().top;
	if (top > 0) {
		top--;
		$("#imgOverlay").css("top", (top) + "px");
	}
	var maxW = $("#originalLogo").width();
	var maxH = $("#originalLogo").height();
	var max = Math.min(maxW, maxH);
	var oldW = $("#imgOverlay").width();
	var oldH = $("#imgOverlay").height();
	if ((left + oldW + 2 <= maxW) && (top + oldH + 2 <= maxH)) {
		$("#imgOverlay").width(oldW + 2);
		$("#imgOverlay").height(oldH + 2);
	}

}

function smaller() {
	var old = $("#imgOverlay").width();
	var min = 100;
	if (old -2 >= min) {
		$("#imgOverlay").width(old -2);
		$("#imgOverlay").height(old -2);
	}
}

function leftMove() {
	var left = $("#imgOverlay").position().left;
	if (left > 0) {
		left = left - 2;
		left = left >= 0 ? left : 0;
		$("#imgOverlay").css("left", (left) + "px");
	}
}

function rightMove() {
	var left = $("#imgOverlay").position().left;
	var old = $("#imgOverlay").width();
	var maxW = $("#originalLogo").width();
	if (left + old + 2 <= maxW) {
		left = left + 2;
//		left = left >= 0 ? left : 0;
		$("#imgOverlay").css("left", (left) + "px");
	}
}


function topMove() {
	var top = $("#imgOverlay").position().top;
	if (top > 0) {
		top = top - 2;
		top = top >= 0 ? top : 0;
		$("#imgOverlay").css("top", (top) + "px");
	}
}

function downMove() {
	var top = $("#imgOverlay").position().top;
	var old = $("#imgOverlay").height();
	var maxH = $("#originalLogo").height();
	if (top + old + 2 <= maxH) {
		top = top + 2;
//		left = left >= 0 ? left : 0;
		$("#imgOverlay").css("top", (top) + "px");
	}
}

function cutFinish() {
	var logoW = $("#originalLogo").width();
	var logoH = $("#originalLogo").height();
	var overlayLeft = $("#imgOverlay").position().left;
	var overlayTop = $("#imgOverlay").position().top;
	var overlayW = $("#imgOverlay").width() + 2;
	var overlayH = $("#imgOverlay").height() + 2;
	window.location = "servlet/logoCut?imgWidthHeight=" + logoW + "," + logoH
		+ "&cutInfo=" + overlayLeft + "," + overlayTop + "," + overlayW
		+ "," + overlayH;
}